/**
 * tv播放
 * 
 * @author luobiao
 */
$(function() {
	show_play_detail.run();
});

var show_play_detail = {
	run : function() {
		this.__init__();
		this._start_();
		this._bind_events();
	},

	__init__ : function() {
		this.id = $("#id").val();
		this.title = $("#htitle").val();
		this.itemUrl = $("#itemUrl").val();
		this.contextPath = $("#contextPath").val();
		main.addOrUpdateViewHistory("TV", this.title, this.id);
		this.vdInstalled = vd.check();
		side_pub._init_side_genre_decade(this);
	},

	_start_ : function() {
		main._play_close_side();
		show_play_detail._get_relates_tv();
	},

	_get_relates_tv: function(){						
		var url = show_play_detail.contextPath + "/tv/full-length/relate/"+this.id;	
		$.get(url, function(result){
			$("#other_video").html(result);
		});
	},	
	
	_bind_events : function() {
		$(".oembed").oembed(null,{
		 	embedMethod: 'fill', 
			maxHeight: 504, 
			maxWidth:980,
			beforeEmbed:show_play_detail.addWModeParameter  
		});
		
		$("#forward").live("click", function() {
			var divId = $(this).attr("divId");
			$(".tv_sList").css("display", "none");
			$("#" + divId).css("display", "block");
		});
		$("#backward").live("click", function() {
			var divId = $(this).attr("divId");
			$(".tv_sList").css("display", "none");
			$("#" + divId).css("display", "block");
		});
		$("#displayEpisode").live("click", function() {
			$("#episodeDetail").toggle();
		});

		$("#episodeDetail > div > ul > li > a").click(function() {
			var $this = $(this);
			var href = $this.attr("href");
			show_play_detail.itemUrl = $this.attr("itemUrl");
			show_play_detail.title = $this.attr("title");
			var genre = show_play_detail.genre;
			var decade = show_play_detail.decade;
			var params = [];
			if (genre) {
				params.push("genre=" + genre);
			}
			if (decade) {
				params.push("decade=" + decade);
			}
			if (params) {
				location.href = href + "#" + params.join("&");
			} else {
				location.href = href;
			}

			return false;
		});

		$("#genre_div > div.vscroll-content > ul > li > a").click(function() {
			show_play_detail.genre = $(this).attr("genre");
			show_play_detail._genre_decade_relocation_event();
			return false;
		});
		$("#decade_div > ul > li > a").click(function() {
			var decadeTemp = $(this).text();
			show_play_detail.decade = decadeTemp.substring(0, 4);
			show_play_detail._genre_decade_relocation_event();
			return false;
		});
		
		var locaIndex = location.href.indexOf("#"); 
		var localUrl = location.href;
		if(locaIndex > 0){
			localUrl = localUrl.substring(0,locaIndex);
		}
		$("#fb-comments").attr("data-href", localUrl);
		$(".loadingBox").show();
		
	},
	
	addWModeParameter:function (oembedData) {     
        if (oembedData.type == "video") {
                var code = oembedData.code;
                var wmode = "transparent";
                if (code != null && code.indexOf("wmode") < 0) {                        
                        code = code.replace("<embed ", "<param name=\"wmode\" value=\"" + wmode + "\"></param>\n<embed ");
                        code = code.replace("<embed ", "<embed wmode=\"" + wmode + "\"");    
                        code = code.replace("<embed ", "<embed allowfullscreen=\"true\"");
                      
                }      
                oembedData.code = code;
        }      
	},

	_genre_decade_relocation_event : function() {
		var genre = show_play_detail.genre;
		var decade = show_play_detail.decade;
		var params = [];
		if (genre) {
			params.push("genre=" + genre);
		}
		if (decade) {
			params.push("decade=" + decade);
		}
		if (!genre && !decade) {
			location.href = main.contextPath + "/tv/full-length/";
			return false;
		}
		location.href = main.contextPath + "/tv/full-lengths#" + params.join("&");
	}

};